/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Executor;

import DataStructures.Globals;
import DataStructures.Request;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Manuel
 */
public class RequestInterpreter {

    public Request interpretQuery(Map parameterMap){
        Request request = new Request();
        //TODO support other operators and constraints
        request.setConcept1(parameterMap.get("concept1").toString().replaceAll(" ", ""));
        request.setConcept2(parameterMap.get("concept2").toString().replaceAll(" ", ""));
        request.setOperator(Globals.OPERATOR_CORRELLATION);
        System.out.println("The query was interpreted as "+request.getConcept1()+" CORRELATION WITH "+request.getConcept2());
        return request;
    }

    public Request interpretQuery(String stringRequest,ArrayList<String> concepts){
        Request request = new Request();
        String[] requestStrings = stringRequest.split(",");
        String c1 = requestStrings[0];
        String c2 = requestStrings[1];
        request.setConcept1(checkSimilarity(c1,concepts));
        request.setConcept2(checkSimilarity(c2,concepts));
        
        return request;
    }

    private static String checkSimilarity(String c1, ArrayList<String> concepts) {
        c1 = c1.replaceAll(" ", "");
        c1 = c1.replaceAll("-", "");
        c1 = c1.replaceAll("_", "").toLowerCase();
        String result = "";
        for(String concept:concepts){
            String test = concept.replaceAll(" ", "").replaceAll("-","").replaceAll("_", "").toLowerCase();

            if(test.equals(c1)){
                result = concept;
                break;
            }
        }
        if(result.equals("")){
            int altHam = 100;
            String alt = "";
            for(String concept: concepts){
                String test = concept.replaceAll(" ", "").replaceAll("-","").replaceAll("_", "").toLowerCase();
                int ham = hamming(c1,test);
                if(ham < altHam){
                    alt = concept;
                    altHam = ham;
                }
            }
            result = alt;
        }
        return result;
    }

    public static String checkSimilarity(String c1, String[] concepts) {
        c1 = c1.replaceAll(" ", "");
        c1 = c1.replaceAll("-", "");
        c1 = c1.replaceAll("_", "").toLowerCase();
        String result = "";
        for(String concept:concepts){
            String test = concept.replaceAll(" ", "").replaceAll("-","").replaceAll("_", "").toLowerCase();

            if(test.equals(c1)){
                result = concept;
                break;
            }
        }
        if(result.equals("")){
            int altHam = 100;
            String alt = "";
            for(String concept: concepts){
                String test = concept.replaceAll(" ", "").replaceAll("-","").replaceAll("_", "").toLowerCase();
                int ham = hamming(c1,test);
                if(ham < altHam){
                    alt = concept;
                    altHam = ham;
                }
            }
            result = alt;
        }
        return result;
    }

    private static int hamming (String request, String realconcept) {
        if(request.length() != realconcept.length()){
            if(request.contains(realconcept) || realconcept.contains(request))
                return Math.abs(request.length() - realconcept.length());
        }
        if(request.length() != realconcept.length()){
            int counter=0;
            String shorter = (request.length() < realconcept.length()) ? request : realconcept;
            for (int k = 0; k < shorter.length();++k)
                if(request.charAt(k) != realconcept.charAt(k))
                    ++counter;
            return counter+Math.abs(request.length() - realconcept.length());
        }
        else{
            int counter = 0;
            for (int k = 0; k < request.length();++k)
                if(request.charAt(k) != realconcept.charAt(k))
                    ++counter;
            return counter;
        }
    }

}
